﻿//作者：       minjie.zhou
// 创建时间：   2012/12/15 11:43:28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AgileToDo.Common;
using AgileToDo.ViewModel;
using YDNoteOpenAPI4N.YDAPI;
using mshtml;

namespace AgileToDo
{
    /// <summary>
    /// WinLoginHide.xaml 的交互逻辑
    /// </summary>
    public partial class WinLoginHide : Window
    {
        public WinLoginHide()
        {
            InitializeComponent();
            this.wb.LoadCompleted += (wb_LoadCompleted);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        public Action LoginSuccess;

        public string UserName { get; set; }
        public string Password { get; set; }

        private void wb_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            var wb = sender as WebBrowser;
            var doc = wb.Document as HTMLDocument;
            if (doc.body == null)
                return;
            if (doc.body.innerHTML.Contains("记住我的登录状态"))
            {
                IHTMLElement user = doc.getElementById("username");
                user.setAttribute("value", UserName);
                IHTMLElement pwd = doc.getElementById("password");
                pwd.setAttribute("value", Password);
                var inputs = doc.getElementsByTagName("input");
                foreach (IHTMLElement input in inputs)
                {
                    input.click();
                }
            }
            else if (doc.body.innerHTML.Contains("访问你的有道云笔记账号"))//模拟点击授权
            {
                IHTMLElement element = doc.getElementById("auth-btn");
                element.click();
            }
            else if (doc.body.innerHTML.Contains("请在程序相应位置输入授权码对AgileToDo进行授权"))
            {
                var list = doc.getElementsByTagName("font");
                foreach (var item in list)
                {
                    var font = item as IHTMLElement;
                    OAuthHelper.VerifierCode = font.innerText.Trim();
                }
                var grantedAccess = OAuthHelper.Consumer.ProcessUserAuthorization(OAuthHelper.RequestToken, OAuthHelper.VerifierCode);
                string accessToken = grantedAccess.AccessToken;
                OAuthHelper.AccessToken = accessToken;

                var api = new YDUserAPI(OAuthHelper.Consumer, OAuthHelper.AccessToken);
                var mainWindowVm = Application.Current.FindResource("mainWindowVM") as MainWindowViewModel;
                if (mainWindowVm != null)
                    mainWindowVm.LoginUser = api.GetUserInfo();

                if (LoginSuccess != null)
                    LoginSuccess();

               // this.Close();

            }
        }
    }
}
